package com.edu.dao.impl;

import com.edu.dao.BaseDao;
import com.edu.dao.UserDao;
import com.edu.entity.User;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImlp extends BaseDao implements UserDao {
    @Override
    public User getByID(int id) {
        String sql = "SELECT * from users WHERE id = " + id;
        System.out.println(sql);
        ResultSet resultSet = super.queryResultSet(sql);
        User u = null;
        try {
            while (resultSet.next()) {
                int id1 = resultSet.getInt("id");
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                String name = resultSet.getString("name");
                String email = resultSet.getString("email");
                String phone = resultSet.getString("phone");
                int state = resultSet.getInt("state");
                int authority = resultSet.getInt("authority");
                u = new User(id1, username, password, name, email, phone, state,authority);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            super.closeAll(conn, ps, resultSet);
        }
        return u;
    }

    @Override
    public User getByUsername(String username) {
        String sql = "SELECT * from users WHERE username = '" + username + "'";
        System.out.println(sql);
        ResultSet resultSet = super.queryResultSet(sql);
        User u = null;
        try {
            while (resultSet.next()) {
                int id1 = resultSet.getInt("id");
                String username1 = resultSet.getString("username");
                String password = resultSet.getString("password");
                String name = resultSet.getString("name");
                String email = resultSet.getString("email");
                String phone = resultSet.getString("phone");
                int state = resultSet.getInt("state");
                int authority = resultSet.getInt("authority");
                u = new User(id1, username1, password, name, email, phone, state,authority);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            super.closeAll(conn, ps, resultSet);
        }
        return u;
    }

    @Override
    public int updateUser(User user) {
        String sql = "UPDATE users SET username = ? , password = ? , name = ? , email = ? , phone = ? WHERE id = ?;";
        Object[] arr = {user.getUsername(), user.getPassword(), user.getName(), user.getEmail(), user.getPhone(), user.getId()};
        int i = BaseDao.executeUpdate(sql, arr);
        return i;
    }

    @Override
    public int addUser(User user) {
        String sql = "INSERT INTO users (username, password, name, email, phone, state) VALUES\n" +
                "(?, ?, ?, ?, ?,?)";
        Object[] arr = {user.getUsername(), user.getPassword(), user.getName(), user.getEmail(), user.getPhone(), user.getState()};
        return BaseDao.executeUpdate(sql, arr);
    }

    @Override
    public User getByUsernameAndPassword(String username, String password) {
        String sql = "SELECT * from users WHERE username = '"+username+"' AND password = '"+password+"'";
        System.out.println(sql);
        ResultSet resultSet = super.queryResultSet(sql);
        User u = null;
        try {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String username1 = resultSet.getString("username");
                String password2 = resultSet.getString("password");
                String name = resultSet.getString("name");
                String email = resultSet.getString("email");
                String phone = resultSet.getString("phone");
                int state = resultSet.getInt("state");
                int authority = resultSet.getInt("authority");
                u = new User(id, username1, password2, name, email, phone, state,authority);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            super.closeAll(conn, ps, resultSet);
        }
        return u;
    }
}
